﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

namespace cadbom
{
    public partial class BasMaterialForm : Form
    {
        NetApi netApi = new NetApi();
        BasMaterial basMaterial = new BasMaterial();
        List<BasMaterialCategory> materialCategoryList = new List<BasMaterialCategory>();
        Action<BasMaterial> callback = null;
        public BasMaterialForm(Action<BasMaterial> callback)
        {
            InitializeComponent();
            this.textBox3.LostFocus += new System.EventHandler(this.textBox3_LostFocus);
            this.textBox4.LostFocus += new System.EventHandler(this.textBox4_LostFocus);
            this.textBox5.LostFocus += new System.EventHandler(this.textBox5_LostFocus);
            this.textBox6.LostFocus += new System.EventHandler(this.textBox6_LostFocus);
            this.textBox7.LostFocus += new System.EventHandler(this.textBox7_LostFocus);
            this.callback = callback;
        }

        private void button1_Click(object sender, EventArgs e)
        {
           
            basMaterial.materialType =  comboBox1.SelectedValue.ToString();
            if (this.textBox2.Text == "")
            {
                MessageBox.Show("物料编码必须输入！");
                return;
            }
            basMaterial.code = this.textBox2.Text;
            if (this.textBox3.Text == "")
            {
                MessageBox.Show("物料名称必须输入！");
                return;
            }
            basMaterial.name = this.textBox3.Text;
            basMaterial.realName = this.textBox3.Text;
            basMaterial.materialVer = this.textBox4.Text;
            basMaterial.model = this.textBox5.Text;
            basMaterial.custCode = this.textBox6.Text;
            basMaterial.description = this.textBox7.Text;
            basMaterial.unitId = comboBox2.SelectedValue.ToString();
            basMaterial.remark = this.textBox9.Text;
            basMaterial.unitId = comboBox2.SelectedValue.ToString();
            basMaterial.status = "1";
            netApi.saveBasMaterial(basMaterial, new Action<BasMaterial>((material) => {
                if (material != null)
                {
                    MessageBox.Show("保存成功！");
                    callback.Invoke(material);
                    this.Close();
                }
            }));
        }
        public void initMatCat(String json)
        {
            List<BasMaterialCategory> list =  JsonConvert.DeserializeObject<List<BasMaterialCategory>>(json);
            List<Object> items = new List<object>();
            materialCategoryList = list;
            list.ForEach(item =>
            {
                items.Add(new { Key = item.id, Value = item.name });
            });
            comboBox1.DataSource = items;
            comboBox1.DisplayMember = "Value"; // 显示的值
            comboBox1.ValueMember = "Key"; // 存储的值（通常用于获取选中的key）
            string typeCode = list[0].code.ToLower();
            getMaterialCode("material_code_" + typeCode);
        }
        public void initUnit(String json)
        {
            List<BasMeasureUnit> list = JsonConvert.DeserializeObject<List<BasMeasureUnit>>(json);
            List<Object> items = new List<object>();

            list.ForEach(item =>
            {
                items.Add(new { Key = item.id, Value = item.name });
            });
            comboBox2.DataSource = items;
            comboBox2.DisplayMember = "Value"; // 显示的值
            comboBox2.ValueMember = "Key"; // 存储的值（通常用于获取选中的key）
        }
        private void getMaterialCode(string typeCode)
        {
            netApi.getMaterialCode(typeCode, new Action<string>(r =>
            {
                if (r == null)
                {
                    MessageBox.Show("类型编码没定义！");
                    this.textBox2.Text = "";
                }
                else
                {
                    this.textBox2.Text = r;
                }
                
            }));
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBox comboBox = sender as ComboBox;
            if (comboBox != null && comboBox.SelectedItem != null)
            {
                string selectedValue = comboBox.SelectedValue.ToString();
                //MessageBox.Show("选中的项是: " + selectedValue);
                BasMaterialCategory basMatCat =  materialCategoryList.FirstOrDefault(p => p.id == selectedValue);
                if (basMatCat != null)
                {
                    string typeCode = basMatCat.code.ToLower();
                    getMaterialCode("material_code_" + typeCode);
                }
            }
        }

        private void BasMaterialForm_Deactivate(object sender, EventArgs e)
        {

        }

        private void textBox3_Enter(object sender, EventArgs e)
        {

        }
        private void checkSameMaterial()
        {
            DataTable dataTable = new DataTable();
            dataTable.Columns.Add("物料编码", typeof(string));
            dataTable.Columns.Add("物料名称", typeof(string));
            dataTable.Columns.Add("物料规格", typeof(string));
            dataTable.Columns.Add("物料描述", typeof(string));
            dataTable.Columns.Add("物料类型", typeof(string));
            
            netApi.getMaterialDataList(basMaterial, new Action<List<BasMaterial>>(r => {
                if (r != null && r.Count > 0)
                {
                    r.ForEach(item =>
                    {
                        dataTable.Rows.Add(item.code,item.realName,item.model,item.description,item.typeName);
                    });
                    this.dataGridView1.DataSource = dataTable;
                }
                else
                {
                    this.dataGridView1.DataSource = dataTable;
                }

            
            }));
        }





        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }
        //物料规格
        private void textBox5_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {

            }

        }
        //物料料号
        private void textBox6_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {

            }
        }
        //物料描述
        private void textBox7_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {

            }
        }
        private void textBox3_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {

            }
        }

        private void textBox4_KeyDown(object sender, KeyEventArgs e)
        {

        }
        private void textBox3_LostFocus(object sender, EventArgs e)
        {
            if (this.textBox3.Text != "")
            {
                basMaterial.realName = this.textBox3.Text;
                checkSameMaterial();
            }
            else
            {
                basMaterial.realName = "";
                checkSameMaterial();
            }
        }

        private void textBox4_LostFocus(object sender, EventArgs e)
        {

        }
        private void textBox5_LostFocus(object sender, EventArgs e)
        {
            if (this.textBox5.Text != "")
            {
                basMaterial.model = this.textBox5.Text;
                checkSameMaterial();
            }
            else
            {
                basMaterial.model = "";
                checkSameMaterial();

            }
        }
        private void textBox6_LostFocus(object sender, EventArgs e)
        {
            if (this.textBox6.Text != "")
            {
                basMaterial.custCode = this.textBox6.Text;
                checkSameMaterial();
            }
            else
            {
                basMaterial.custCode = "";
                checkSameMaterial();
            }
        }
        private void textBox7_LostFocus(object sender, EventArgs e)
        {
            if (this.textBox7.Text != "")
            {
                basMaterial.description = this.textBox7.Text;
                checkSameMaterial();
            }
            else
            {
                basMaterial.description = "";
                checkSameMaterial();
            }
        }
    }
}
